package com.edu.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edu.demo.entity.ProResume;
import com.edu.demo.entity.Project;
import com.edu.demo.exception.MultipleException;
import com.edu.demo.pojo.AjaxResult;
import com.edu.demo.service.IProResumeService;
import com.edu.demo.service.IProjectService;
import com.edu.demo.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * 项目简历投递
 * </p>
 *
 * @author jixuewei
 * @since 2023-01-12
 */
@RestController
@RequestMapping("/proResume")
public class ProResumeController {

    @Autowired
    private IProjectService projectService;

    @Autowired
    private IProResumeService resumeService;

    /**
     * 学生查询自己投递的项目
     * @return
     */
    @GetMapping("/list/recruited")
    @PreAuthorize("@ps.hasPerm('stu:project:recruited')")
    public AjaxResult projectsOfRecruited(){

        List<Project> projectsOfRecruited = projectService.projectsOfRecruited();
        return AjaxResult.success(projectsOfRecruited);
    }

    /**
     * 学生查询项目id=?是否已投递
     * @return
     */
    @GetMapping("/id/recruited")
    @PreAuthorize("@ps.hasPerm('stu:project:recruited')")
    public AjaxResult recruitedOrNot(@RequestParam Integer id){
        List<ProResume> list = resumeService.list(new QueryWrapper<ProResume>().eq("project_id", id).eq("stu_id", SecurityUtils.getStudent().getId()));
        if(list.isEmpty()) return AjaxResult.success(false);//未投递
        return AjaxResult.success(true);//已投递
    }

    /**
     *学生投递项目
     * @param resume
     * @return
     */
    @PostMapping("/submit")
    @PreAuthorize("@ps.hasPerm('stu:proResume:insert')")
    public AjaxResult submitResume(@RequestBody ProResume resume){
        int i = 0;
        try {
            resume.setStuId(SecurityUtils.getStudent().getId());
            i = resumeService.insertResume(resume);
        } catch (Exception e) {
            e.printStackTrace();
            if(e instanceof MultipleException || e instanceof IllegalStateException){
                return AjaxResult.error(e.getMessage());
            }else return AjaxResult.error();
        }
        return AjaxResult.success(i==1);
    }

    /**
     * 教师查看录用淘汰简历
     * @param resume
     * @return
     */
    @PostMapping("/judge")
    @PreAuthorize("@ps.hasPerm('tea:resume:update')")
    public AjaxResult judgeResume(@RequestBody ProResume resume){
        int i = resumeService.judgeResume(resume);
        return AjaxResult.success(i==1);
    }

    /**
     *教师点击录用返回学生、项目信息
     */
    @GetMapping("/infos")
    @PreAuthorize("@ps.hasPerm('resume:info:query')")
    public AjaxResult resumeInfos(@RequestParam Integer projectId,@RequestParam Integer stuId){
        try {
            Map<String, Object> map = resumeService.resumeInfos(projectId, stuId);
            return AjaxResult.success(map);
        }catch (NullPointerException e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 教师查看项目下投递的简历
     * @param projectId
     * @return
     */
    @GetMapping("/list/recruiting")
    @PreAuthorize("@ps.hasPerm('tea:resume:list')")
    public AjaxResult list(@RequestParam Integer projectId,@RequestParam Integer index,@RequestParam Integer size){
        AjaxResult success = AjaxResult.success();
        resumeService.list(success,index,size,projectId);
        return success;
    }
}
